﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using System.Net;
using HslCommunication.Core;
using System.Net.Sockets;
using HslCommunication.BasicFramework;

namespace HslCommunication.Core.Net
{
    public class NetworkDoubleBase<TNetMessage, TTransform> : NetworkBase, IDisposable where TNetMessage : INetMessage, new() where TTransform : IByteTransform, new()
    {
        private TTransform byteTransform;                // 数据变换的接口
        private string ipAddress = "127.0.0.1";          // 连接的IP地址
        private int port = 10000;                        // 端口号

        protected bool isPersistentConn = false;         // 是否处于长连接的状态
        protected bool IsSocketError = false;            // 指示长连接的套接字是否处于错误的状态
        private int connectTimeOut = 10000;              // 连接超时时间设置


        public TTransform ByteTransform
        {
            get { return byteTransform; }
            set { byteTransform = value; }
        }

        public virtual int Port
        {
            get
            {
                return port;
            }
            set
            {
                port = value;
            }
        }


        public OperateResult ConnectServer()
        {
            isPersistentConn = true;
            OperateResult result = new OperateResult();

            // 重新连接之前，先将旧的数据进行清空
            CoreSocket?.Close();

            OperateResult<Socket> rSocket = CreateSocketAndInitialication();

            if (!rSocket.IsSuccess)
            {
                IsSocketError = true;
                rSocket.Content = null;
                result.Message = rSocket.Message;
            }
            else
            {
                CoreSocket = rSocket.Content;
                result.IsSuccess = true;
                LogNet?.WriteDebug(ToString(), StringResources.Language.NetEngineStart);
            }

            return result;
        }

        private OperateResult<Socket> CreateSocketAndInitialication()
        {
            OperateResult<Socket> result = CreateSocketAndConnect(new IPEndPoint(IPAddress.Parse(ipAddress), port), connectTimeOut);
            if (result.IsSuccess)
            {
                // 初始化
                OperateResult initi = InitializationOnConnect(result.Content);
                if (!initi.IsSuccess)
                {
                    result.Content?.Close();
                    result.IsSuccess = initi.IsSuccess;
                    result.CopyErrorFromOther(initi);
                }
            }
            return result;
        }

        protected virtual OperateResult InitializationOnConnect(Socket socket)
        {
            return OperateResult.CreateSuccessResult();
        }


        public virtual string IpAddress
        {
            get
            {
                return ipAddress;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    if (!IPAddress.TryParse(value, out IPAddress address))
                    {
                        throw new Exception(StringResources.Language.IpAddresError);
                    }
                    ipAddress = value;
                }
                else
                {
                    ipAddress = "127.0.0.1";
                }
            }
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
